// export let httpUrl = '/api'  // 接访问地址

export let httpUrl = "api"
// export let httpUrl = "https://api.ysjcp.com"

import { getAuthorization, setAuthorization, removeAuthorization } from "@/utils/auth.js"

export function get(url, data) {
    let tokenObj = getAuthorization("token") ? JSON.parse(getAuthorization("token")) : ""
    let token = tokenObj.AccessToken
    let promise = new Promise((resolve, reject) => {
        uni.request({
            url: httpUrl + url,
            data,
            method: 'GET',
            dataType: 'json',
            header: {
                'content-type': 'application/json',
                "Authorization": "Bearer " + token
            },
            success: function (res) {
                if (res.statusCode != 200) {
                    throwFuc(res)
                    reject(res)
                } else {
                    if (res.statusCode == 200 && res.data.code != 200) {
                        // throwFuc(res.data)
                        if(res.data.code == 401){
                            loginError(res)
                        }
                        reject(res.data)
                    } else {
                        resolve(res.data)
                    }
                }
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
    return promise
}

export function post(url, data) {
    let tokenObj = getAuthorization("token") ? JSON.parse(getAuthorization("token")) : ""
    let token = tokenObj.AccessToken
    let promise = new Promise((resolve, reject) => {
        uni.request({
            url: httpUrl + url,
            data,
            method: 'POST',
            dataType: 'json',
            header: {
                'content-type': 'application/json',
                "Authorization": "Bearer " + token
            },
            success: function (res) {
                if (res.statusCode != 200) {
                    throwFuc(res)
                    reject(res)
                } else {
                    if (res.statusCode == 200 && res.data.code != 200) {
                        if(res.data.code == 401){
                            loginError(res)
                        }
                        reject(res.data)
                    } else {
                        resolve(res.data)
                    }
                }
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
    return promise
}

export function patch(url, data) {
    let tokenObj = getAuthorization("token") ? JSON.parse(getAuthorization("token")) : ""
    let token = tokenObj.AccessToken
    let promise = new Promise((resolve, reject) => {
        uni.request({
            url: httpUrl + url,
            data,
            method: 'patch',
            dataType: 'json',
            header: {
                'content-type': 'application/json',
                "Authorization": "Bearer " + token
            },
            success: function (res) {
                if (res.statusCode != 200) {
                    throwFuc(res)
                    reject(res)
                } else {
                    if (res.statusCode == 200 && res.data.code != 200) {
                        if(res.data.code == 401){
                            loginError(res)
                        }
                        reject(res.data)
                    } else {
                        resolve(res.data)
                    }
                }
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
    return promise
}

export function put(url, data) {
    let tokenObj = getAuthorization("token") ? JSON.parse(getAuthorization("token")) : ""
    let token = tokenObj.AccessToken
    let promise = new Promise((resolve, reject) => {
        uni.request({
            url: httpUrl + url,
            data,
            method: 'PUT',
            dataType: 'json',
            header: {
                'content-type': 'application/json',
                "Authorization": "Bearer " + token
            },
            success: function (res) {
                if (res.statusCode != 200) {
                    throwFuc(res)
                    reject(res)
                } else {
                    if (res.statusCode == 200 && res.data.code != 200) {
                        if(res.data.code == 401){
                            loginError(res)
                        }
                        reject(res.data)
                    } else {
                        resolve(res.data)
                    }
                }
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
    return promise
}

export function del(url, data) {
    let tokenObj = getAuthorization("token") ? JSON.parse(getAuthorization("token")) : ""
    let token = tokenObj.AccessToken
    let promise = new Promise((resolve, reject) => {
        uni.request({
            url: httpUrl + url,
            data,
            method: 'DELETE',
            dataType: 'json',
            header: {
                'content-type': 'application/json',
                "Authorization": "Bearer " + token
            },
            success: function (res) {
                if (res.statusCode != 200) {
                    throwFuc(res)
                    reject(res.data)
                } else {
                    if (res.statusCode == 200 && res.data.code != 200) {
                        if(res.data.code == 401){
                            loginError(res)
                        }
                        reject(res.data)
                    } else {
                        resolve(res.data)
                    }
                }
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
    return promise
}

const throwFuc = (data) => {
    // uni.showToast({
    //     title: '网络错误，请联系管理员',
    //     icon: "none",
    //     duration: 2000
    // });
}

// 登录异常
const loginError = (res) => {
    if(res.data.code == 401){
        removeAuthorization('token')
        uni.navigateTo({
            url:'/pages/login/mobileNumberLogin/index'
        })
    }
}